package com.ting.test.algorithms.二叉树;

import java.util.*;

/**
 * Created by 雷霆 on 2020/3/15.
 * <p>
 * Description：
 */
public class DLRTes2 {
    public static int[] levelOrder(final TreeNode root) {

        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.add(root);
        List<Integer> list = new ArrayList();
        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            list.add(node.val);
            if (null != node.left) {
                queue.add(node.left);
            }
            if (null != node.right) {
                queue.add(node.right);
            }

        }
        int[] result = new int[list.size()];
        for (int i = 0; i < result.length; i++) {
            result[i] = list.get(i);
        }
        return result;
    }


    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(3);
        TreeNode treeNode1 = new TreeNode(9);
        TreeNode treeNode2 = new TreeNode(20);
        TreeNode treeNode3 = new TreeNode(15);
        TreeNode treeNode4 = new TreeNode(7);

        treeNode2.setLeft(treeNode3);
        treeNode2.setRight(treeNode4);
        treeNode.setLeft(treeNode1);
        treeNode.setRight(treeNode2);
        int[] result = levelOrder(treeNode);
        for (int i = 0; i < result.length; i++) {
            System.out.println(result[i]);
        }
    }
}
